//
//  HyqMeController.swift
//  约麻
//
//  Created by rimi on 2017/7/12.
//  Copyright © 2017年 Sweet. All rights reserved.
//
import UIKit
import AVOSCloud
import AFNetworking
import MBProgressHUD


class HyqMeController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource {
    @IBOutlet weak var tableview: UITableView!
    @IBOutlet weak var bgheard: UIImageView!
    @IBOutlet weak var nickName: UILabel!
    @IBOutlet weak var heardImg: UIImageView!
    var cameraImg = UIImagePickerController()
    var photoImg = UIImagePickerController()
    var dataArry = ["我的好友","帮助","关于我们","清除缓存","设置"]
    var imgArry = ["img.jpg","img.jpg","img.jpg","img.jpg","img.jpg"]
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.isNavigationBarHidden = true
        if AVUser.current() != nil{
            let nickname = AVUser.current()?.object(forKey: "name")
            self.nickName.text = nickname as? String
            let heard =   AVUser.current()?.object(forKey: "headimage")
            guard let imageurl = heard as? String else{
                return
            }
            self.heardImg.sd_setImage(with: URL(string: imageurl))
        }
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // 圆角
        bgheard.layer.cornerRadius = 40
        bgheard.layer.masksToBounds = true
        heardImg.layer.cornerRadius = 35
        heardImg.layer.masksToBounds = true
 //
        cameraImg.delegate = self
        photoImg.delegate = self
        tableview.delegate = self
        tableview.dataSource = self
        tableview.separatorStyle = .none
        tableview.isScrollEnabled = false
        }
//上传头像
    @IBAction func heardImgBtn(_ sender: UIButton) {
        print("更换头像")
        if  UserDefaults.standard.bool(forKey: "islogin") == false {
            let vv = UIStoryboard(name: "ZWHLogin", bundle: nil).instantiateViewController(withIdentifier: "ZWHLoginViewController") as! ZWHLoginViewController
            self.present(UINavigationController(rootViewController: vv), animated: true, completion: nil)
        }
        else{
            phootoPicker()
        
        }
      
    }
   //MARK: - 头像上传
    func phootoPicker()  {
     let alert = UIAlertController(title: "", message:"选择上传头像", preferredStyle: .actionSheet)
        alert .addAction(UIAlertAction(title: "取消", style:.cancel, handler: { (UIAlertAction)in
            print("取消了")
        }))
        alert.addAction(UIAlertAction(title: "相册中选取", style: .default, handler: { (UIAlertAction) in
            print("相册")
            self.photoImg.sourceType = .photoLibrary
            // 照片是否编辑
            self.photoImg.allowsEditing = true
            self.present(self.photoImg, animated: true, completion: nil)
            
        }))
        alert.addAction(UIAlertAction(title: "拍一张上传", style: .default, handler: { (UIAlertAction) in
           print("拍照")
         let isok = UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
            if isok {
            self.cameraImg.sourceType = .camera
            self.present(self.cameraImg, animated: true, completion: nil)
            }
        }))
        // 弹出
        self.present(alert, animated: true, completion: nil)
    }
    // MARK - UIImagePickerController&&delegat
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print("照片选择完成")
        MBProgressHUD.showAdded(to: view, animated: true)
        //上传头像
        let ima = info[UIImagePickerControllerEditedImage] as! UIImage
        let data = UIImagePNGRepresentation(ima)
        let file = AVFile(name: "heardImgPNG", data: data!)
        file.saveInBackground { [weak self](succes, err) in
            if succes == true{
                MBProgressHUD.hideAllHUDs(for: self?.view, animated: true)
                //上传
                AVUser.current()?.setObject(file.url, forKey: "headimage")
                AVUser.current()?.save()
                ZWHAlertController.showAlert(vc: self!, title: "上传成功", message: "", style: .alert)
            }
            else {
              //  print(err ?? "")
                MBProgressHUD.hideAllHUDs(for: self?.view, animated: true)
                  ZWHAlertController.showAlert(vc: self!, title: "上传失败", message: "", style: .alert)
            }
        }

        picker .dismiss(animated: true, completion: nil)
    }
    @IBAction func modifyNickename(_ sender: UIButton) {
   
//        let vv = UIStoryboard(name: "Me", bundle: nil).instantiateViewController(withIdentifier: "HyqModifyController") as! HyqModifyController
//        self.present(UINavigationController(rootViewController: vv), animated: true, completion: nil)
    }
    
    // MARK : tableViewdelgate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArry.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "meCell", for: indexPath) as! HyqMeCell
        cell.lab.text = dataArry[indexPath.row]
        let ima = UIImage(named: imgArry[indexPath.row])
        //cell.img.image = ima
        cell.accessoryType = .disclosureIndicator
        cell.selectionStyle = .none
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 1 {
            let vv =  HyqHelpController()
            self.present(UINavigationController(rootViewController: vv), animated: true, completion: nil)
        }
        if indexPath.row == 4 {
            let vv = UIStoryboard(name: "Me", bundle: nil).instantiateViewController(withIdentifier: "HyqModifyController") as! HyqModifyController
            self.present(UINavigationController(rootViewController: vv), animated: true, completion: nil)
        }
        if indexPath.row == 2 {
            let vv = UIStoryboard(name: "Me", bundle: nil).instantiateViewController(withIdentifier: "HyqaboutMainController") as! HyqaboutMainController
            self.present(UINavigationController(rootViewController: vv), animated: true, completion: nil)
        }
        if indexPath.row == 0{
            let vc = UIStoryboard(name: "ZWHUserlistViewController", bundle: nil).instantiateViewController(withIdentifier: "ZWHUserlistViewController") as? ZWHUserlistViewController
            let nav = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController
            nav?.pushViewController(vc!, animated: true)
        }
    }
    
}





